<?php

namespace App\Logics;


use App\Models\News;
use App\Models\Student;
use App\Service\Base\Logic;

class NewsLogic extends Logic
{

    // 社区：文章

    // 列表查询
    public function onLists()
    {
        $opt   = $this->option();
        $page  = $opt->getData('page', 1, 'intval');
        $rows  = $opt->getData('rows', 15, 'intval');
        $start = ($page - 1) * $rows;


        // 模型 和 查询  ->isCache($this->listCache)
        $option  = $this->option();
        $where   = [['verify', '=', 1]];
        $bang    = ['verify'];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $s_id    = $option->getData('student_id', 0, 'intval');
        $is_top  = $option->getData('is_top', null, 'intval');
        $_verify = $option->getData('verify', null, 'intval');
        $_type   = $option->getData('event_type', null, 'trim');
        $title   = $option->getData('title', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($s_id){
            $bang[]  = 'student_id';
            $where[] = ['student_id', '=', $s_id];
        }
        if($_type){
            $bang[]  = 'event_type';
            $where[] = ['event_type', '=', $_type];
        }
        if(!is_null($is_top)){
            $bang[]  = 'is_top';
            $where[] = ['is_top', '=', $is_top];
        }
        if($title){
            $bang    = false;
            $where[] = ['title', 'like', "%".$title."%"];
        }
        if($keyword){
            $where[] = $this->orWhere('title|description|address', 'like', $keyword);
            $bang    = false;
        }

        // 模型
        $model  = new News();
        $KCache = $model->cacheKey('lists', $where);
        $lists  = $model->cacheClass()->getCache($KCache);
        // 检查是否已经绑定
        if(!empty($lists)){
            $model->checkBangList($lists, $KCache);
        }

        if(empty($lists)){
            //$result = $model->bangField($bang)->listPage($where, $page, $rows, "id DESC");
            $lists = $model->bangField($bang)->getList($where, $rows, "id DESC", $start);
            $lists = is_object($lists) ? $lists->toArray() : $lists;
            if($where != false){
                $model->addCacheList($lists, $KCache, $lists, 'list');
            }
        }

        return $lists;
    }

    // 软删除
    public function toSoft($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where  = ['id' => $_id, 'uid' => $myUid];
        $model  = new News();
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 改变（直接修改，软删除，改变状态，排序）
    public function onEdit($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定修改的ID编号！');
        }

        $opt  = $this->option();
        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'title'       => $opt->getData("title", null, "trim"),
            'uid'         => $myUid,
            'cover'       => $opt->getData("cover", null, "trim"),
            'keyword'     => $opt->getData("keyword", null, "trim"),
            'description' => $opt->getData("description", null, "trim"),
            //'read_no'     => $opt->getData("read_no", null, "intval"),
            //'virtual_no'  => $opt->getData("virtual_no", null, "intval"),
            //'praise_no'   => $opt->getData("praise_no", null, "intval"),
            //'collect_no'  => $opt->getData("collect_no", null, "intval"),
            //'relay_no'    => $opt->getData("relay_no", null, "intval"),
            //'comment_no'  => $opt->getData("comment_no", null, "intval"),
            //'images'      => $opt->getData("images", null, "trim"),
            'verify'      => $opt->getData("verify", 1, "intval"),
            'is_top'      => $opt->getData("is_top", null, "intval"),
            'push_time'   => $opt->getData("push_time", null, "intval"),
            //'ip'          => $opt->getData("ip", null, "trim"),
            //'ip_address'  => $opt->getData("ip_address", null, "trim"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 组合数据
        $where = ['id' => $_id, 'uid' => $myUid];
        $model = new News();
        $data  = $model->checkData($data, $where);
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $result = $model->checkRules($data, 'edit');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneEdit($where, $data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 树状
    public function onAdd($myUid)
    {
        $opt  = $this->option();
        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'title'       => $opt->getData("title", null, "trim"),
            'uid'         => $myUid,
            'cover'       => $opt->getData("cover", null, "trim"),
            'keyword'     => $opt->getData("keyword", null, "trim"),
            'description' => $opt->getData("description", null, "trim"),
            //'read_no'     => $opt->getData("read_no", null, "intval"),
            //'virtual_no'  => $opt->getData("virtual_no", null, "intval"),
            //'praise_no'   => $opt->getData("praise_no", null, "intval"),
            //'collect_no'  => $opt->getData("collect_no", null, "intval"),
            //'relay_no'    => $opt->getData("relay_no", null, "intval"),
            //'comment_no'  => $opt->getData("comment_no", null, "intval"),
            //'images'      => $opt->getData("images", null, "trim"),
            //'ip'          => $opt->getData("ip", null, "trim"),
            //'ip_address'  => $opt->getData("ip_address", null, "trim"),
            'verify'      => $opt->getData("verify", 1, "intval"),
            'is_top'      => $opt->getData("is_top", null, "intval"),
            'push_time'   => $opt->getData("push_time", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];


        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 组合数据
        $model = new News();
        $data  = $model->checkData($data);
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $result = $model->checkRules($data, 'add');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneAdd($data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

}


